package com.cskaoyan.controller.ControllerByshj;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.PeriMissionListMapper;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.AdminServiceImpl;
import com.cskaoyan.util.GetLogedAdmin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//shiro整合之后，在做具体的开发
//响应结果都是JSON，Controller组件上我们都用@RestController注解
@RestController
@RequestMapping("admin/auth")
public class AuthController {
    /**
     * 如果参数比较少，类型比较简单的话，使用map来接收也可以
     */
    @Autowired
    PeriMissionListMapper periMissionListMapper;

    @PostMapping("login")
    public BaseRespVo<LoginUserData> login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        //整合shiro
        //获得操作主体
        Subject subject = SecurityUtils.getSubject();
        //这下面这行会执行config包下面的Custom下的里面的方法
        subject.login(new UsernamePasswordToken(username, password));
        //这上面一步把得到的username ,password传入进行比对
        //这里还有一个异常没有处理，万一没有通过还会出现一些异常
        Admin logedAdmin = GetLogedAdmin.getLogedAdmin();
        username = logedAdmin.getUsername();
        System.out.println(logedAdmin.getPassword());
        LoginUserData loginUserData = new LoginUserData();
        AdminInfoBean adminInfo = new AdminInfoBean();
        adminInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        adminInfo.setNickName(username);
        loginUserData.setAdminInfo(adminInfo);
        loginUserData.setToken((String) subject.getSession().getId());
        return BaseRespVo.ok(loginUserData);
    }

    @RequestMapping("info")
    public BaseRespVo info(String token) {
        Admin admin = GetLogedAdmin.getLogedAdmin();
        Integer[] roleIds=null;
        try {
          roleIds = admin.getRoleIds();
        }
       catch (NullPointerException e){
            throw new AuthorizationException();
       }

        List<String> perms = new ArrayList<>();
        for (int i = 0; i < roleIds.length; i++) {
//            PeriMissionListExample periMissionListExample = new PeriMissionListExample();
//            PeriMissionListExample.Criteria criteria = periMissionListExample.createCriteria();
//            criteria.andRoleIdEqualTo(roleIds[i]);
            List<String> periMissionLists = periMissionListMapper.selectFromPerMissionDetail(roleIds[i]);
            perms.addAll(periMissionLists);
        }
        InfoData infoData = new InfoData();
        infoData.setName(admin.getUsername());
        infoData.setAvatar(admin.getAvatar());
        infoData.setRoles(Arrays.asList(admin.getRoleIds()));
        //如果包含的有1那么权限为*
        for (int i = 0; i < roleIds.length; i++) {
            if (roleIds[i] == 1) {
                ArrayList<String> biggestPower = new ArrayList<>();
                biggestPower.add("*");
                infoData.setPerms(biggestPower);
                return BaseRespVo.getResult(infoData, "成功", 0);
            }

        }
        infoData.setPerms(perms);
        return BaseRespVo.getResult(infoData, "成功", 0);
    }

    @PostMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return BaseRespVo.getResult(null, "成功", 0);
    }
}
